package com.example.zhaojianxu.signin.car.detail

import android.os.Bundle
import android.view.View
import com.example.zhaojianxu.signin.R
import com.example.zhaojianxu.signin.bean.BaseActivity
import com.example.zhaojianxu.signin.car.http.CarEntity
import com.example.zhaojianxu.signin.common.constance.ComConstance
import com.example.zhaojianxu.signin.common.http.HttpUtil
import com.example.zhaojianxu.signin.common.util.ToastUtil
import kotlinx.android.synthetic.main.activity_car_detail.*
import kotlinx.android.synthetic.main.common_title_bar.*
import retrofit2.Call
import retrofit2.Callback
import retrofit2.Response

class CarDetailActivity : BaseActivity() {

    var data : CarEntity? = null

    override fun initView() {
        common_back.setOnClickListener {
            finish()
        }

        common_title.text = "车辆信息"
        common_setting.visibility = View.GONE
    }

    override fun initData() {
        data = intent.getSerializableExtra("data") as? CarEntity

        activity_car_detail_1.text = data?.name
        activity_car_detail_3.text = data?.detail
        activity_car_detail_4.setOnClickListener {
            leaseCar()
        }
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_car_detail)
    }


    fun leaseCar(){
        val cid = data?.cid
        if (cid == null){
            finish()
        }

        HttpUtil.instance.getCarApi()?.postLeaseCar(ComConstance.UID,cid!!)?.enqueue(object: Callback<String>{
            override fun onFailure(call: Call<String>, t: Throwable) {
            }

            override fun onResponse(call: Call<String>, response: Response<String>) {
                if (response.isSuccessful){
                    ToastUtil.instance.showToast(this@CarDetailActivity,"租赁成功!")
                }
            }

        })
    }
}
